package lp.xian.production.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.domain.Saleout;
import com.ruoyi.common.mapper.SaleoutMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import lp.xian.production.domain.ReturnInfo;
import lp.xian.production.mapper.ReturnInfoMapper;
import lp.xian.production.vo.MarketVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.mapper.MarketMapper;
import com.ruoyi.common.domain.Market;
import lp.xian.production.service.IMarketService;

/**
 * 销售列表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-06
 */
@Service
public class MarketServiceImpl implements IMarketService
{
    @Autowired
    private MarketMapper marketMapper;
   /* @Autowired
    private FinanceMapper financeMapper;*/
    @Autowired
    private ReturnInfoMapper returnInfoMapper;
    @Autowired
    private SaleoutMapper saleoutMapper;

    /**
     * 查询销售列表
     *
     * @param marketId 销售列表主键
     * @return 销售列表
     */
    @Override
    public Market selectMarketByMarketId(Integer marketId)
    {
        return marketMapper.selectMarketByMarketId(marketId);
    }

    /**
     * 查询销售列表列表
     *
     * @param market 销售列表
     * @return 销售列表
     */
    @Override
    public List<Market> selectMarketList(Market market)
    {
        return marketMapper.selectMarketList(market);
    }

    /**
     * 新增销售列表
     *
     * @param market 销售列表
     * @return 结果
     */
    @Override
    public int insertMarket(Market market)
    {
        market.setCreateTime(DateUtils.getNowDate());
        return marketMapper.insertMarket(market);
    }

    /**
     * 修改销售列表
     *
     * @param market 销售列表
     * @return 结果
     */
    @Override
    public int updateMarket(Market market)
    {
        market.setUpdateTime(DateUtils.getNowDate());
        return marketMapper.updateMarket(market);
    }

    /**
     * 批量删除销售列表
     *
     * @param marketIds 需要删除的销售列表主键
     * @return 结果
     */
    @Override
    public int deleteMarketByMarketIds(Integer[] marketIds)
    {
        return marketMapper.deleteMarketByMarketIds(marketIds);
    }

    /**
     * 删除销售列表信息
     *
     * @param marketId 销售列表主键
     * @return 结果
     */
    @Override
    public int deleteMarketByMarketId(Integer marketId)
    {
        return marketMapper.deleteMarketByMarketId(marketId);
    }

    @Override
    public int auditMarket(MarketVo marketVo) {
        Market market =new Market();
        market.setMarketId(marketVo.getMarketId());
        market.setAuditStatus(marketVo.getAuditStatus());
        market.setAuditOpinion(marketVo.getAuditOpinion());
        market.setAuditPerson(SecurityUtils.getUsername());
        market.setAuditTime(new Date());
        marketMapper.updateMarket(market);
        Market market1=selectMarketByMarketId(marketVo.getMarketId());
        Saleout saleout=new Saleout();
        saleout.setOrderNo(market1.getOrderNo());
        saleout.setOrderName(market1.getOrderName());
        saleout.setClientId(market1.getClientId());
        saleout.setMarketNum(market1.getMarketNum());
        saleout.setMarketPrice(market1.getMarketPrice());
        saleout.setMarketUnit(market1.getMarketUnit());
        saleout.setMarketTotal(market1.getMarketTotal());
        saleout.setMarketTax(market1.getMarketTax());

        saleoutMapper.insertSaleout(saleout);
        return 1;
    }

    @Override
    public int approverMarket(MarketVo marketVo) {
        Market market =new Market();
        market.setMarketId(marketVo.getMarketId());
        market.setAuditStatus(marketVo.getAuditStatus());
        market.setApproverPerson(SecurityUtils.getUsername());
        market.setApproverTime(new Date());
        int num=marketMapper.updateMarket(market);
      /*  if (num>0){
            Finance yu=financeMapper.selectFinanceBalance();//查余额
            Market market1=marketMapper.selectMarketByMarketId(marketVo.getMarketId());
            Finance finance=new Finance();
            finance.setFinanceName("【销售定金】"+market1.getMarketName());
            double doubleValue=market1.getMarketPrice().doubleValue();
            double nums=market1.getMarketNum();
            double money=doubleValue * nums*Constants.Order.Subscription.RATIO;
            BigDecimal fmoney=new BigDecimal(money);
            finance.setFinanceBalance(yu.getFinanceBalance());
            finance.setFinanceMoney(fmoney);
            finance.setFinanceStatus(1);
            finance.setClientId(market1.getClientId());
            finance.setCreateTime(new Date());
            finance.setCreateBy("系统生成");
           return financeMapper.insertFinance(finance);
        }*/
        return 1;
    }

    @Override
    public int returnMarket(MarketVo marketVo) {
        Market market =new Market();
        market.setMarketId(marketVo.getMarketId());
        market.setOrderStatus(Integer.parseInt(marketVo.getOrderStatus()));
        int num=marketMapper.updateMarket(market);
        if (num>0){
            Market market1=marketMapper.selectMarketByMarketId(marketVo.getMarketId());
            ReturnInfo returnInfo=new ReturnInfo();
            returnInfo.setReturnName(market1.getMarketName());
            returnInfo.setReturnContent(marketVo.getReturnContent());
            returnInfo.setReturnOrderNo(marketVo.getReturnOrderNo());
            returnInfo.setClientNum(Integer.parseInt(marketVo.getClientNum()));
            returnInfo.setMarketUnit(market1.getMarketUnit());
            returnInfo.setMarketPrice(market1.getMarketPrice());
            returnInfo.setClientId(market1.getClientId());
            returnInfo.setCreateTime(new Date());
            returnInfo.setCreateBy("系统生成");
            return returnInfoMapper.insertReturnInfo(returnInfo);
        }
        return 0;
    }
}
